package pers.mingshan.thread.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 线程池
 * @author mingshan
 *
 */
public class ThreadPoolDemo {

    public static void main(String[] args) {
        //创建线程池
        //固定的线程池
        //ExecutorService threadPool=Executors.newFixedThreadPool(3);

        //不固定的线程池
        ExecutorService threadPool2=Executors.newCachedThreadPool();
        for (int i = 0; i  <10; i++) {
            threadPool2.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("ThreadName-"+Thread.currentThread().getName());
                }
            });
        }

        //利用线程池使用定时器
        Executors.newScheduledThreadPool(2).scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("zzz");
            }
        }, 6, 2, TimeUnit.MINUTES);
    }
}
